import { Promise } from 'core-js';
import http from './http';
import {password} from '../utils/encryption'
import _ from 'lodash'

function savePerson(data){
  const params=_.cloneDeep(data);
  params.password=password(params.password).toString().toLocaleUpperCase()
  return http.post("/api/factory/admin/save",params).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  });
}

function getPersons(data){
  return http.post("/api/factory/admin/query/page",data).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  })
}

function delPerson(id){
  return http.get("/api/factory/admin/del",{
    params:{
      id:id
    }
  }).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  })
}

function getFactorysByName(name=undefined){
  return http.get("/api/factory/query/names",{
    params:{
      name:name
    }
  }).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  });
}

function saveFactory(data){
  return http.post("/api/factory/save",data).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  });
}

function getFactorys(data){
  return http.post("/api/factory/query/page",data).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  });
}

function delFactory(id){
  return http.get("/api/factory/del",{
    params:{
      id:id
    }
  }).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  })
}

function getRecords(data){
  return http.post("/api/visitor/register/page",data).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  })
}

function login(data){
  const params=_.cloneDeep(data);
  params.password=password(params.password).toString().toLocaleUpperCase()
  return http.post("/api/admin/login",params).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  })
}

function uplaodFile(file){
  const formData = new FormData();
  formData.append('file', file);
  return http.post("/api/file/upload",formData).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  })
}

function saveQRCode(data){
  return http.post("/api/qr_code/save",data).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  })
}

function getQRCodes(data){
  return http.post("/api/qr_code/page",data).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  })
}

function delQRCodes(id){
  return http.get("/api/qr_code/del",{
    params:{
      id
    }
  }).then(res =>{
    if(res.code==200){
      return Promise.resolve(res.data);
    }
  })
}

function fileUrl(code){
  return `${process.env.VUE_APP_API_BASE}/api/file/download?key=${code}`;
}

function downloadFile(code){
  return http.get("/api/file/download",{
    params:{
      key:code
    },
    responseType:"blob"
  })
}

export {
  savePerson,getFactorys,getPersons,delPerson,delFactory,getFactorysByName,saveFactory,getRecords,login,uplaodFile,saveQRCode,getQRCodes,delQRCodes,fileUrl,downloadFile
}